<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>双端队列</title>
</head>

<body>
    <!-- 双端队列：double end queue 队首队尾可插入-->
    <script>
        // addFront(element) 在双端队前添加元素
        // addBack(element) 在双端队后添加元素
        // removeFront() 移除双端队前第一元素
        // removeBack() 移除双端队后第一元素
        // peekFront() 返回双端队前第一元素
        // peekBack() 返回双端队后第一元素

        class Deque {
            constructor() {
                this.item = [];
            }
            addFront(element) { //前端插入
                this.item.unshift(element);
            }
            addBack(element) { //后端插入
                this.item.push(element);
            }
            removeFront() { //前端删除
                return this.item.shift();
            }
            removeBack() { //后端删除
                return this.item.pop();
            }
            peekFront() { //前端返回
                if (this.item.length === 0) {
                    return undefined;
                } else {
                    return this.item[0];
                }
            }
            peekBack() { //后端返回
                if (this.item.length === 0) {
                    return undefined;
                } else {
                    return this.item[this.item.length - 1];
                }
            }
            toString() {
                return this.item.toString();
            }
            isEmpty() {
                // return this.item.length === 0;
                return this.size() === 0;
            }
            size() {
                return this.item.length;
            }
        }
        const deque = new Deque();
        deque.addBack("blue");
        deque.addBack("red");
        deque.addFront("json");
        console.log(deque.toString());
        console.log(deque.peekFront());
        console.log(deque.peekBack());
        console.log(deque.removeFront());
        console.log(deque.removeBack());
        console.log(deque.size());
        console.log(deque.isEmpty());
    </script>
</body>

</html>